
// Copyright (C) Recursion Company. All rights reserved.

#ifndef __VOL_RPC_SERVICE_H__
#define __VOL_RPC_SERVICE_H__

#ifdef __OS_LINUX_VER
	#include <pthread.h>
	#include <unistd.h>
    #include <sys/time.h>
	#include <sys/types.h>
	#include <sys/socket.h>
	#include <netinet/in.h>
	#include <sys/time.h>
	#include <netdb.h>
	#include <fcntl.h>

    #define __stdcall __attribute__((stdcall))
	#include "vol_mtypes.h"
#else
    #include <wtypes.h>
	#include <process.h>
	#include <winsock.h>
#endif

#include "vol_rpc_mem.h"
#include "volOSFunction.h"
#include "vol_locker.h"
#include "volElementDef.h"
#include "volWorkThreadPool.h"
#include "volSendThreadPool.h"
#include "volRecvThreadPool.h"
#include "volClientWorkThreadPool.h"
#include "volThreadPool.h"
#include "volTaskQueue.h"
#include "volResultQueue.h"
#include "volFunClient.h"
#include "volSendMsgList.h"
#include "volClientList.h"
#include "volMidResultQueue.h"
#include "volFunSocket.h"
#include "vol_rpc_service.h"

#define _VOL_STATIC static

//---------------------------------------------------------------------------------------

namespace VOL_RPC
{

void GetSendTextData (const TCHAR* szSendText, INT nCharSet, CU8String* pu8strBuf, CVolMem* pmemBuf, BYTE** ppbSendData, INT_P* pnpSendDataSize);

}

//---------------------------------------------------------------------------------------

class CXmlRpcServer : public VOL_RPC::SERVERDATA
{
public:
    CXmlRpcServer ();
    ~CXmlRpcServer ();

public:
    BOOL StartServer (INT nPort, BOOL blWorkMode, VOL_RPC::WORKPROC proc, INT_P npUserData);
    void StopServer ();
    void SetThreadPoolSize (INT nMinSize, INT nMaxSize);
    BOOL GetReqText (INT_P npMessageAddress, CVolString& strReqCode, CVolString& strReqMessage, INT nCharSet);
    BOOL GetReqBin (INT_P npMessageAddress, CVolString& strReqCode, CVolMem& memReqMessage);

    BOOL SendTextMessageToClient (const TCHAR* szClientCode, INT_P npClientHandle, const TCHAR* szSendText, INT nCharSet);
    BOOL SendBinMessageToClient (const TCHAR* szClientCode, INT_P npClientHandle, const BYTE* pbSendData, INT_P npSendDataSize);
    inline_ BOOL SendBinMessageToClient (const TCHAR* szClientCode, INT_P npClientHandle, const CVolMem& memData)
    {
        return SendBinMessageToClient (szClientCode, npClientHandle, memData.GetPtr (), memData.GetSize ());
    }

    BOOL SendTextMessageToAllClient (const TCHAR* szSendText, INT nCharSet, INT_P npExcludeClientHandle);
    BOOL SendBinMessageToAllClient (const BYTE* pbSendData, INT_P npSendDataSize, INT_P npExcludeClientHandle);
    inline_ BOOL SendBinMessageToAllClient (const CVolMem& memData, INT_P npExcludeClientHandle)
    {
        return SendBinMessageToAllClient (memData.GetPtr (), memData.GetSize (), npExcludeClientHandle);
    }

    INT_P GetClientHandle (INT_P npMessageAddress);
    BOOL GetClientIPAddress (INT_P npClientHandle, CVolString& strClientIPAddress);
    INT GetClientCount ();
    BOOL GetClientHandleArray (CMArray<INT_P>& aryClientHandles);
    BOOL DisconnectClient (INT_P npClientHandle);
    BOOL DisconnectAllClient (INT_P npExcludeClientHandle);
    INT GetClientMessageType (INT_P npMessageAddress);

    INT_P GetClientUserData (INT_P npClientHandle);
    BOOL SetClientUserData (INT_P npClientHandle, INT_P npUserData);
};

//---------------------------------------------------------------------------------------

class CXmlRpcClient : public VOL_RPC::CLIENTDATA
{
public:
    CXmlRpcClient ();
    ~CXmlRpcClient ();

public:
    BOOL ConnectServer (INT nPort, const TCHAR* szServerAddress, BOOL blSyncReqMode, VOL_RPC::CLIENTWORKPROC proc, INT_P npUserData);
    void DisconnectServer ();

    INT SyncSendText (const TCHAR* szSendText, CVolString& strResult, INT nMaxWaitTime, INT nSendTextCharset, INT nResultTextCharset);
    INT SyncSendBin (const BYTE* pbSendData, INT_P npSendDataSize, CVolMem& memResult, INT nMaxWaitTime);
    inline_ INT SyncSendBin (const CVolMem& memSendData, CVolMem& memResult, INT nMaxWaitTime)
    {
        return SyncSendBin (memSendData.GetPtr (), memSendData.GetSize (), memResult, nMaxWaitTime);
    }

    BOOL AsyncSendText (const TCHAR* szSendText, INT nSendTextCharset);
    BOOL AsyncSendBin (const BYTE* pbSendData, INT_P npSendDataSize);
    inline_ BOOL AsyncSendBin (const CVolMem& memSendData)
    {
        return AsyncSendBin (memSendData.GetPtr (), memSendData.GetSize ());
    }

    BOOL GetResultText (INT_P npMessageAddress, CVolString& strResult, INT nCharSet);
    BOOL GetResultBin (INT_P npMessageAddress, CVolMem& memResult);
    INT GetServerMessageType (INT_P npMessageAddress);
    inline_ BOOL IsConnected ()  {  return (g_bStopClient == FALSE);  }
};

#endif
